#coding:utf8
from routin_lock import *
from routin_alt import *
from routin_scroll import *
from routin_enter import *

def zi_to_len():
    if cfg.sum=='0.':
        x1_kuai()
        return
    
    if cfg.sum=='0.0':
        do_type_reload()
        set_cfg_sum_0()
        return
    
    if is_no_move():
        return
    a,d = change_sum(cfg.sum)
    set_cfg_sum_0()
    if not a:
        d = -d        
    if not cfg.w2showing:
        dotdot()
    set_scroll_len(d*get_sc_size())  

def do_into_x2():   
    into_x2_mode()
    cfg.sum=str(cfg.far[0])
    deal_x2_mode_num()        

def reset_x2_ini_and_slow():
    cfg.x2_ini_mouse=0
    cfg.x2_mode_slow = not cfg.x2_mode_slow

def rightDouble():    
    if not kuai_ge_chg :
        enter_ge_mode(1)
        cfg.getokuai=False
    else:
        enter_ge_mode(3)
        cfg.getokuai=100
        
def x1_click():  #右键
    if cfg.x2_mode:
        reset_x2_ini_and_slow()
        return
    if  cfg.sum not in ('0','0.','0.0') and( is_no_move() or (not cfg.w2showing and float(cfg.sum)<1) ) :
        do_type(cfg.sum)
        set_cfg_sum_0()
        return
    
    if cfg.sum !='0':
        zi_to_ge()
        return
    if cfg.min_ge!=0 and cfg.w2showing and not cfg.min_ge_force_show:
        cfg.min_ge_force_show = True
        return
    if cfg.w2showing:
        if not cfg.x2_mode:
            update_du(True)        
        return
    if cfg.left_sign==0:
        cfg.left_sign=1
        jump_state_chg(0)
        deal_pressed()
        return
    
    if cfg.min_ge!=0 and is_no_move() and cfg.force_show and not cfg.w2showing and not cfg.min_ge_force_not_show:
        if cfg.min_ge==1:
            print('gg',cfg.eg_from_mid)
            if not cfg.eg_from_mid:
                deal_pressed()
                return
            enter_ge_mode(2)
            deal_pressed()
            
            
        elif cfg.min_ge==3:
            clear(True)
            rightDouble()
        else:
            ge_add_x2(cfg.min_ge)
        return    
            
    if cfg.min_ge!=0 and not cfg.min_ge_force_show:
        cfg.min_ge_force_show = True                 
        if not cfg.w2showing:
            dotdot()
        dd2 = cal_dis(getp(),cfg.fix_pos)
        if  dd2>=min_d  and dd2>=ge_wait_dis and not cfg.min_ge_has_refresh:        
            cfg.min_ge_has_refresh = True
            toto,b = get_ge(dd2)
            cfg.to = toto*dd2/ge_wait_dis    
            cfg.to_change_time = time.time()   
        return
    
    if cfg.jump_kuai!=0:
        acc_total()        
        cfg.right_sign=0
        return
    
    if not is_jump_base() and cfg.jump_state==1:
        jump_state_chg(0)
        clear_jump()
        clear_acc()
        cfg.can_set_acc=True
        cfg.right_sign=0
        return
    if  cfg.acc_speed == g_mouse_speed[2]:
        acc_down(True)
        cfg.right_sign=0
        return
    if cfg.right_sign==0:
        cfg.right_sign=1
        clear(True)
        rightDouble()
        return
    
    ge_x2()    
    cfg.right_sign=0

def x2_change_sum():
    if cfg.sum=='0.':
        d1,d2 = get_last_idx()
        cfg.sum = str(d1)
        return
    if cfg.sum=='0.0':
        d1,d2 = get_last_idx()
        cfg.sum = str(d2)
        return

def zi_to_ge():
    x2_change_sum()
    if cfg.sum[:2]=='0.':
        cfg.sum = cfg.sum.replace('0.','0')
        if cfg.sum=='':
            cfg.sum='0'
        deal_manu_scroll(-round(float(cfg.sum)))

    if cfg.sum[-1]=='.':
        deal_manu_scroll(round(float(cfg.sum)))

    elif cfg.sum[-2:]=='.0':
        deal_manu_scroll(-round(float(cfg.sum)))
    else:
        d = round(float(cfg.sum))
        set_fix_ge_len(d)        

    if not cfg.w2showing:
        dotdot()        
    set_cfg_sum_0()    
def delete_extra_zero(n):
    '''删除小数点后多余的0'''
    if isinstance(n, int):
        return n
    if isinstance(n, float):
        n = str(n).rstrip('0')  # 删除小数点后多余的0
        n = int(n.rstrip('.')) if n.endswith('.') else float(n)  # 只剩小数点直接转int，否则转回float
        return n
def ge_x2():
    ori=cfg.min_ge    
    if ori==0:
        pp = get_cur_pos()
        cfg.last_m = (pp[0]-cfg.fix_pos[0],pp[1]-cfg.fix_pos[1] )      
    clear(True)
    cfg.show_one_on = True
    cfg.force_show = True
    ge_add_x2(ori)
def x2_e_chg():
    if not cfg.w2showing:
        return
    a = get_snap_sort()
    if len(a)<2:
        return
    a1 = int(a[-1][0])
    a2 = int(a[-2][0])
    if is_ff(cfg.sum):
        dd = int(cfg.far[0])-(a1-a2)*float(cfg.sum)
        cfg.sum = str(delete_extra_zero(dd))
        return
    if cfg.show_mode_plus>0 or not cfg.showCut:
        return
    dd = int(cfg.far[0])+(a1-a2)*float(cfg.sum)
    cfg.sum = str(delete_extra_zero(dd))

def gege(ignore_pre=False):
    if cfg.min_ge==leftUpGe and is_no_move() and  cfg.eg_from_mid:
        y_all_left(True)
        return
    if cfg.min_ge !=1 and not cfg.w2showing and  cfg.sum=='0' and cfg.min_ge!=0 and not cfg.min_ge_force_show and is_no_move() and cfg.show_one_on:        
        ge_add_ctrl(cfg.min_ge)        
        return True
    
    if  not ignore_pre:               
        if cfg.jump_state!=0:
            if cfg.left_sign==0:
                cfg.left_sign=1
                cfg.jump_from_click=False
                cfg.can_break=True
                cfg.right_mouse_time=0
                change_left_jump()
                return
            cfg.left_sign=0
            jump_state_to_man()
            cfg.is_after_man=True
            return
        if cfg.left_sign==0:
            cfg.left_sign=1
            enter_ge_mode(1)
            cfg.getokuai=False
            return

        if is_acc_up():
            if cfg.ori_kuai!=1:
                cfg.ori_kuai=1
            else:
                cfg.ori_kuai=0
            cfg.left_sign=0
            return
        if cfg.jump_kuai!=0:
            if cfg.jump_kuai==1:
                kuai_into_2()
            else:
                kuai_into_2()
                cfg.jump_kuai=1
            cfg.left_sign=0
            return
        cache_gap()
        enter_ge_mode(1)
        cfg.left_sign=0
        return    

    if not cfg.w2showing and cfg.sum=='0':
        ori = cfg.min_ge
        if ori==0:
            cache_gap()
    
        clear(True)
        cfg.show_one_on = True
        cfg.force_show = True
        ge_add_xing(ori)
        if ori==0:
            ge_set(leftUpGe)
            cfg.eg_from_mid=True

def x2_noshow():
    if cfg.sum=='0.':
        switch_show_pos()
        return
    move_p(int(float(cfg.sum)))    
    
def x2_click():  #左键   
    if cfg.x2_mode:
        x2_jump()     
        return       
    
    if cfg.sum != '0':
        if not cfg.w2showing:
            x2_noshow()
            set_cfg_sum_0()
            return
        x2_e_chg()
        deal_e(True)
        return
        
    if cfg.w2showing:
        do_into_x2()
        return
        
    gege()
                          
def x_all():
    if cfg.w2showing:
        if not cfg.x2_mode:
            switch_showcut()
        else:
            exit_x2_mode()       
        return     
    clear(True)        
    if cfg.x2_type==1:        
        deal_pressed()
    else:
        enter_ge_mode(4)

def y_all_left(er=False):   
    if er:
        set_to_last(True)
    no_to_3()

    
def exit_one_level(exitdu=False):
    if cfg.x2_mode:
        exit_x2_plain(exitdu)
        return    

    if is_sca():
        du_exit()
        return
    
    clear(True)

def y_all():
    if cfg.x2_type==0:#左键
        if not cfg.w2showing:
            y_all_left()
            return        
        cfg.x2_mode=False
        clear(True)
        return
    
    if not cfg.w2showing:
        if cfg.mid_co>cfg.x2_co:
            enter_ge_mode(5)        
        else:
            enter_ge_mode(2)        
            deal_pressed()

        return
    exit_one_level()
            
def xscro():
    a = get_snap_sort()
    d=-1
    if len(a)<=1 or not cfg.w2showing:
        if cfg.x2_type==1:
            d=10
        else:
            d=20
    if d>0:
        set_fix_ge_len(d)
    if d<0 and cfg.x2_type==0:
        clear(True)
        return
    if not cfg.w2showing:
        dotdot()
    cfg.show_mode=0
    cfg.to_change_time=time.time()

def x_double(st):     
    if 'x2' in st:
        if cfg.has_update_cancle and cfg.has_update_cancle_counter:
            cfg.has_update_cancle = False
        return True
    elif cfg.has_update_cancle  and cfg.has_update_cancle_counter:    
        x2_jump()
        return False
    if cfg.has_update_cancle:
        return False
            
    x2_jump()
    return False
    
def x_click(button, pressed):    
    if pressed:      
        cfg.x2_ini_x2_mode=False
        cfg.x2_mode_exit_time = 0
        if 'x1' in str(button):
            cfg.x2_type = 1
            cfg.pause_mode=False
            cfg.has_update = False
            cfg.has_update_cancle = False
            cfg.has_update_has_wheel = False
            if cfg.x2_mode:
                cfg.has_update_cancle = True
                cfg.has_update_cancle_counter = False
                cfg.x2_ini_x2_mode = True            
        else:
            cfg.pause_mode = True            
            cfg.x2_type = 0        

        cfg.x_press_time = time.time()
        if cfg.middle_pressed:
            cfg.x2_pressed_then_other = True
            cfg.middle_pressed_other = True
            y_all()
            return
        
        if pm.scroll_pressed:
            pm.scroll_other = True
            cfg.x2_pressed_then_other = True
            xscro()
            return
        
        if not cfg.x2_pressed:
            cfg.x2_pressed = True
            cfg.x2_pressed_then_other = False
            
        else:
            cfg.x2_pressed_then_other = True
            if cfg.is_suo_middle:
                cfg.suo_middle_to_scal = False
                cfg.suo_middle_exit_co +=1
            elif cfg.x2_mode and cfg.has_update:                          
                cfg.has_update_cancle = not cfg.has_update_cancle
                cfg.has_update_cancle_counter = False
            else:
                x_all()                
        return
    cfg.jump_dx[0] = cfg.jump_dx[0]//2
    cfg.jump_dx[1] = cfg.jump_dx[1]//2
    if time.time()-cfg.x_press_time>x_cancel_gap:
       cfg.x2_pressed_then_other = True
    exit_suo()
    if cfg.x2_mode and not cfg.x2_pressed_then_other:
        pass
    else:
        cfg.x2_mode_slow=False
    if cfg.x2_pressed_then_other:
        set_cfg_sum_0()

    if cfg.has_update:
        if x_double(str(button)):
            return

    cfg.has_update = False
    cfg.has_update_cancle = False
    cfg.x2_pressed =False
    
    if  cfg.x2_pressed_then_other:
        # cfg.jump_kuai=0

        return
    
    if 'x1' in str(button):
        x1_click()
        return
    
    x2_click()                                                                    

def midDecode():
    if cfg.sum[-1]=='.':
        return True,False,float(cfg.sum)
    if cfg.sum[-2:]=='.0':
        return False,True,float(cfg.sum)
    if cfg.sum[-3:]=='.00':
        return False,False,float(cfg.sum)            
    if cfg.sum[:2]=='0.':
        a = cfg.sum[2:]
        rev = True
        if a[0]=='0':
            a = a[1:]
            rev = False
        if a=='':
            a = '0'
        return False,rev,float(a)
    return True,True,float(cfg.sum)

def zi_to_alt():
    isx,a,ff = midDecode()
    a,dsx,dsy = alt4_dx(isx,(a,ff))
    if not a:
        return
    x2, y2 = getp(None,False)        
    sx, sy = cfg.fix_pos
    dx = x2 - sx 
    dy = y2 - sy      
    if isx:
        n = round(abs( (dsx-sx)/dx ))
    else:
        n = round(abs( (dsy-sy)/dy ))
    n = max(0,n-1)
    set_fix_ge_len(n)

def zi_to_xy():
    su = cfg.sum
    if is_no_move():
        set_to_last(True)
    cfg.sum = su
    if not cfg.w2showing:
        dotdot()
    zi_to_alt()
    set_cfg_sum_0()

def midScroll(is_real=False):
    if cfg.w2showing:     
        cfg.x2_mode=False
        clear(True)        
    else:
        if not is_real:
            ctrl2_com()
            return
        cfg.pause_key = not cfg.pause_key

def exit_suo():
    if cfg.is_suo_middle:
        cfg.suo_middle_exit_co -=1
        if cfg.suo_middle_exit_co<=0:
            cfg.is_suo_middle = False
            con_set_pos(cfg.suo_middle_start_pos[0],cfg.suo_middle_start_pos[1])        
            if not is_sca() and cfg.w2showing_for_suo:
                update_du(True)
    if cfg.is_suo and cfg.w2showing_for_suo:
        update_du(True)
    cfg.is_suo = False

def deal_mid(pressed):
    if pressed:
        cfg.x2_mode_exit_time = 0
    if cfg.x2_pressed and pressed:
        if not cfg.x2_pressed_then_other:
            cfg.x2_pressed_then_other = True
            cfg.middle_pressed_other = True        
            y_all()
        cfg.x2_pressed_then_other = True
        cfg.middle_pressed_other = True        
        return True
    
    if pressed and pm.scroll_pressed:
        pm.scroll_other = True
        cfg.middle_pressed_other = True
        cfg.pause_key = not cfg.pause_key
        return True
    if pressed and cfg.ctrl2_job:
        up_double()
        cfg.ctrl2_other=True
        cfg.middle_pressed_other = True
        return
    if pressed:
        return True    
    if time.time()-cfg.x_press_time>x_cancel_gap:
        cfg.middle_pressed_other = True
    exit_suo()
    if cfg.middle_pressed_other:
        return

    if cfg.left_sign==0:
        y_all_left(True)
        cfg.left_sign=1
        jump_state_chg(0)
        return
    if cfg.left_sign2==0:
        enter_force()
        cfg.left_sign2=1
        return
    if cfg.man_sign==0:
        up_double()
        return
    
    if cfg.min_ge==leftUpGe and is_no_move():
        enter_ge_mode(1)
        cfg.getokuai=False
        return
    
    if cfg.min_ge==3 and is_no_move():
        y_all()
        return
    if is_acc_up():
        acc_down(True)
        return
    
    if not cfg.w2showing and cfg.sum=='0': 
        if cfg.jump_kuai==2:
            cfg.jump_kuai=1
            return
        if cfg.jump_kuai==1:
            cfg.jump_kuai=0
            return
        if  not cfg.force_show:
            gege(True)
            return                
        xing()
        return    

    if cfg.sum!='0' and  not cfg.x2_mode:        
        if cfg.sum=='0.':
            x3_kuai()
            set_cfg_sum_0()
            return True
        if cfg.sum=='0.0':
            x2_kuai()
            set_cfg_sum_0()
            return True
        if is_no_move():    
            return        
        zi_to_xy()                
        return True
    
    if cfg.w2showing:
        cfg.middle_pressed_other = True
        if cfg.x2_mode:
            x2_jump()
            cfg.click_time = time.time()
        else:
            ctrl2()
        
    return True

g_pre_co=0
g_re_co =0
def click_clear(clear_lock):
    cfg.x2_mode = False       
    if time.time()-cfg.x2_mode_exit_time>0.01:
        cfg.x2_mode_exit_time = 0 

    else:
        cfg.isShow = False
    if cfg.always_show_one:      
        cfg.show_one_on = True
    else:
        cfg.show_one_on = False    
    clear(True,clear_lock)

def on_click2(x, y, button, pressed,deal_x):    
    cfg.lock_keep_step = False        
    if str(button) in ('Button.middle','Button.right'):
        return      
    from mm import is_left_leftBtn
    if  not ig_left() and str(button) == 'Button.left':
        if is_left_leftBtn():
            return 
    
    cfg.last_active_time = time.time()
    if ('x1' in str(button) or 'x2' in str(button)):
        if deal_x:
            x_click(button, pressed)
        return
    if str(button) != 'Button.left':
        cfg.is_pressed = False
        click_clear(True)
        return
    if get_cur_pos()==cfg.j_p and pressed:
        print('hit jp',cfg.tt_co)
        if cfg.tt_co<2*right_jump_thre:
            cfg.tt_co+=1
    global g_pre_co,g_re_co
    if pressed:
        if g_re_co<g_pre_co:
            cfg.release_job = time.time()
            print('rjob',g_re_co,g_pre_co,cfg.release_job)
        g_pre_co +=1        
    else:
        g_re_co+=1   
        if g_re_co<g_pre_co:
            cfg.release_job = time.time()
            print('rjob',g_re_co,g_pre_co,cfg.release_job)

    if not pressed:
        cfg.is_pressed = False    
    ori = cfg.force_show and cfg.min_ge==0
    click_clear(not pressed)
    if pressed:
        cfg.force_show = ori
